package com.imau.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.imau.bean.SysDept;
import com.imau.service.SysdeptService;
import com.imau.service.impl.SysdeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/sysdept/*")
public class SysdeptConterllor extends Baseconterllor {

    private SysdeptService sysdeptService = new SysdeptServiceImpl();


    /**
     * 添加教师
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String jsonString = request.getReader().readLine();

        //转成对象
        SysDept SysDept = JSON.parseObject(jsonString, SysDept.class);

        //3.保存
        SysDept entiry = sysdeptService.save(SysDept);

        //4.返回响应

        this.writeResult(response, entiry == null ? false : true);


    }


    /**
     * 删除
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void removeById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String did = request.getParameter("did");


        //删除
        int row = sysdeptService.deleteByDid(did);
        //4.返回响应

        this.writeResult(response, row > 0 ? true : false);


    }


    protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String jsonString = request.getReader().readLine();

        //转成对象
        SysDept dept = JSON.parseObject(jsonString, SysDept.class);

        //3.保存
        int row = sysdeptService.updateDept(dept);

        //4.返回响应

        this.writeResult(response, row > 0 ? true : false);


    }


    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String jsonString = request.getReader().readLine();

        //转成对象
        SysDept SysDept = JSON.parseObject(jsonString, SysDept.class);

        String currentPage = request.getParameter("currentPage");

        String pageSize = request.getParameter("pageSize");

        //3.保存

        PageInfo<SysDept> pageInfo = sysdeptService.queryListByPagte(SysDept, Integer.valueOf(currentPage), Integer.valueOf(pageSize));

        //4.返回响应
        writeResultAndData(response, pageInfo);


    }



    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void listAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String json = request.getReader().readLine();

        SysDept sysDept = JSON.parseObject(json, SysDept.class);

        List<SysDept> sysDepts=null;
        if (sysDept!=null)
        {
            sysDepts  = sysdeptService.queryList(sysDept);

        }else {
            sysDepts  = sysdeptService.queryList(new SysDept());
        }


        //4.返回响应
        writeResultAndData(response, sysDepts);


    }
    protected void getInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String did = request.getParameter("did");


        SysDept SysDept = sysdeptService.findById(did);

        //4.返回响应
        writeResultAndData(response, SysDept);


    }

}
